const fs = require('fs')
const Nightmare = require('nightmare');


const nightmare = Nightmare({
  waitTimeout: 60 * 1000 * 2,
  show: true,
  openDevTools: {
    mode: 'detach'
  },
});



const page = nightmare.goto('https://m.baidu.com/tcx?appui=alaxs&data=%7B%22fromaction%22%3A%22search_zhongjianye67%22%2C%22fromaction_original%22%3A%22search_zhongjianye67%22%2C%22query%22%3A%22剑来%22%7D&page=detail&gid=14294994339&sign=656e401b56e102a957dcba9f5c59c37c&ts=1659119591&sourceurl=http%3A%2F%2Fwww.xuanjiezhimen.org%2F8_8225%2F&trace_log=undefined_undefined_undefined&cid=14294994339%7C1571610379&url=http%3A%2F%2Fwww.lwm99.com%2Ftxt%2F73671%2F59616556.html')
page.wait(3000)
  .url()
  .then(async url => {
    console.log(`Crawling ${url}...`);
    let nextFlag = true
    let txtStr = ''
    // while (nextFlag) {
    try {
      await page.wait(2000)
      const {
        contents,
        hasNextPage
      } = await page.evaluate(() => {
        let contents = ''
        let hasNextPage = true
        const textTitle = document.querySelector('.chapter-content .chapter-title');
        const textContent = document.querySelectorAll('.chapter-content .read-content');

        contents = textTitle.innerText + '\n' + Array.from(textContent).reduce((str, el) => {
          return str + el.innerText
        })
        

        const nextPageButton = document.querySelector('.toolbar-box .toolbar-arrow .flow-right')
        if (nextPageButton.className.indexOf('disable')) {
          hasNextPage = false
        } else {
          hasNextPage = true
        }
        console.log(contents, hasNextPage);
        return {
          contents,
          hasNextPage
        }
      });

      txtStr += (contents + '\n')
      nextFlag = hasNextPage

      page.click('.flow-right')
      page.wait('3000')
      

    } catch (error) {
      console.error(`Failed to crawl ${url}: ${error}`);
    }
    // }

    fs.writeFile('./teststr.txt', txtStr, function (err) {
      if (err) {
        return console.log(err + 'txt写入失败的');
      }
      console.log('txt写入成功');
    })
    nightmare.end();
  })
  .catch((error) => {
    console.error('Search failed:', error);
    nightmare.end();
  });